package com.dayaokeji.rhythmschoolstudent.wiget

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AppCompatDialogFragment
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.dayaokeji.rhythmschoolstudent.R
import java.io.Serializable

/**
 *********************************************************************
 **  Created by Zeno on 2018/7/27
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 **
 *********************************************************************
 */
class ConfirmDialog : AppCompatDialogFragment(), View.OnClickListener {

    companion object {
        private const val CONFIRM_INFO = "confirm_info"
        fun newInstance(confirmInfo: ConfirmInfo): ConfirmDialog {
            val bundle = Bundle()
            val confirmDialog = ConfirmDialog()
            bundle.putSerializable(CONFIRM_INFO,confirmInfo)
            confirmDialog.arguments = bundle
            return confirmDialog
        }
    }

    private lateinit var confirmInfo: ConfirmInfo
    private lateinit var tvTitle: TextView
    private lateinit var tvContent: TextView
    private lateinit var btnConfirm: Button
    private var confirmClickListener: ConfirmClickListener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.window.setBackgroundDrawable(ColorDrawable())
        val view = inflater.inflate(R.layout.dialog_confirm_layout, container, false)
        tvTitle = view.findViewById(R.id.tv_title)
        tvContent = view.findViewById(R.id.tv_content)
        btnConfirm = view.findViewById(R.id.btn_confirm)
        btnConfirm.setOnClickListener(this)
        setStyle(DialogFragment.STYLE_NO_TITLE,theme)
        view.minimumHeight = (activity?.resources?.displayMetrics?.heightPixels!! * 0.28).toInt()
        view.minimumWidth = (activity?.resources?.displayMetrics?.widthPixels!! * 0.8).toInt()
        return view
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        confirmInfo = arguments?.getSerializable(CONFIRM_INFO) as ConfirmInfo

        tvTitle.text = confirmInfo.title
        tvContent.text = Html.fromHtml(confirmInfo.content)
    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                R.id.btn_confirm -> {
                    if (confirmClickListener != null) {
                        confirmClickListener?.click()
                        dismiss()
                    }
                }
            }
        }
    }

    fun setConfirmClickListener(confirmClickListener: ConfirmClickListener) {
        this.confirmClickListener = confirmClickListener
    }

    interface ConfirmClickListener{
        fun click()
    }

    data class ConfirmInfo(val title: String = "提示", val content: String) : Serializable
}